package com.lantosec.hmrp.modules.eval.controller.app;


import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.eval.entity.EvalTaskEvent;
import com.lantosec.hmrp.modules.eval.service.IEvalTaskEventService;
import com.lantosec.hmrp.modules.eval.vo.EvalTaskEventVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * 考评任务项目事件 控制器
 *
 * @author AutoCode
 * @since 2020-03-19
 */
@Api(value = "考评任务项目事件控制器APP端", tags = "考评任务项目事件控制器APP端")
@RestController
@RequestMapping("/eval/app/task/event")
@Log4j2
public class EvalTaskEventAppController extends BaseController {
    @Autowired
    private IEvalTaskEventService service;


    @GetMapping("queryItemHistoryTask")
    @ApiOperation(value = "查询考评项目历史事件")
    public Result<List<EvalTaskEventVO>> queryItemHistoryTask(@ApiParam(value = "考评任务开始时间", required = true) @RequestParam(name = "startDate") String startDate,
                                                              @ApiParam(value = "考评任务结束时间", required = true) @RequestParam(name = "endDate") String endDate,
                                                              @ApiParam(value = "考评任务明细id", required = true) @RequestParam(name = "evalDetailedId") Integer evalDetailedId,
                                                              @ApiParam(value = "考评项目id", required = true) @RequestParam(name = "evalItemId") String evalItemId,
                                                              @ApiParam(value = "考评对象id", required = true) @RequestParam(name = "planTargetId") Integer planTargetId){
        List<EvalTaskEventVO> evalTaskEventListVo = service.queryItemHistoryTask(startDate,endDate,evalDetailedId,evalItemId,planTargetId);
        return Result.ok(evalTaskEventListVo);
    }

    @GetMapping("queryTargetTask/{evalDetailedId}")
    @ApiOperation(value = "查询考评对象下的所有事件")
    public Result<PageVO<EvalTaskEventVO>> queryTargetTask(@ApiParam(value = "分页对象", required = true) PageVO<EvalTaskEvent> page,
                                                           @ApiParam(value = "考评明细ID", required = true) @PathVariable(name = "evalDetailedId") String evalDetailedId){
        PageVO<EvalTaskEventVO> evalTaskEventListVo = service.queryTargetTask(page,evalDetailedId);
        return Result.ok(evalTaskEventListVo);
    }

    @PostMapping("create")
    @ControllerLog(name = "考评任务添加事件", type = LogType.add)
    @ApiOperation(value = "添加事件")
    public Result<Integer> create(@ApiParam(value = "考评任务项目事件VO对象", required = true) @RequestBody @Validated EvalTaskEventVO vo, BindingResult bindingResult){
        validationHandler(bindingResult);
        service.saveEvalTaskEvent(vo);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{id}")
    @ControllerLog(name = "逻辑删除考评任务事件", type = LogType.delete)
    @ApiOperation("逻辑删除考评任务事件")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="id") String id){
        if(StringUtils.isNotEmpty(id)){
            service.deleteTaskEvent(id);
        }
        return Result.ok();
    }
}
